<?php

global $mydb;
connect();
set_time_limit(999999999999);
 //test();
//die("X");

function connect(){
	global $mydb;
	$mydb = new wpdb('phanmem_upmnhanh','Ne2ZWWWK7gkl','phanmem_pmmnhanh','118.69.201.244');
	//$mydb = new wpdb('root','123','download.com.vn','localhost');
}


/*
	TRANS CATEGORY
*/
//insert_category();

/*
	TRANS PUBLISHER
*/
//insert_publisher();
//insert_license();

insert_post();

function insert_category(){
	//insert_category_lv1();
	//insert_category_lv2();
	//insert_category_lv3();
}

function insert_license() {
	global $mydb;
	$rows = $mydb->get_results("select * from license");
	foreach($rows as $r){
		$rterm = wp_insert_term(
			  $r->LICE_NAME, // the term 
			  'license'
		);

		if(!is_wp_error($rterm)){
		$error_string = $rterm->get_error_message();
		print_r($error_string);
			//$query = "update `license` set TERM_ID= '".$rterm['term_id']."' where LICE_ID='".$r->LICE_ID."'"; 
          // $mydb->query($query);
		   die("");
		    echo $r->LICE_NAME."<br>";
		}
		else {
		//$query = "update `license` set TERM_ID= '".$rterm['term_id']."' where LICE_ID='".$r->LICE_ID."'"; 
           //$mydb->query($query);
		}
	}
}

function insert_publisher() {
	global $mydb;
	$rows = $mydb->get_results("select * from manufacturer where TERM_ID is null");
	foreach($rows as $r){
		$rterm = wp_insert_term(
			  $r->MAN_NAME, // the term 
			  'publisher'
		);

		if(!is_wp_error($rterm)){
			$query = "update `manufacturer` set TERM_ID= '".$rterm['term_id']."' where MAN_ID='".$r->MAN_ID."'"; 
           $mydb->query($query);
		   
		    echo $r->MAN_NAME."<br>";
		}
		else {
			$newName = $r->MAN_NAME." 2";
			$query = "update `manufacturer` set `MAN_NAME` = '".$newName."' where MAN_ID='".$r->MAN_ID."'"; 
           $mydb->query($query);
		  
		}
	}
}

function insert_category_lv1(){
	global $mydb;
	$rows = $mydb->get_results("select * from category where CAT_PARENT = 0");
	foreach($rows as $r){
		$rterm = wp_insert_term(
			  $r->CAT_NAME, // the term 
			  'category'// the taxonomy
			  
		);

		if(!is_wp_error($rterm)){
			$query = "update `category` set TERM_ID= '".$rterm['term_id']."' where CAT_ID='".$r->CAT_ID."'"; 
           $mydb->query($query);
		   
		   $query = "update `category` set TERM_PARENT= '".$rterm['term_id']."' where CAT_PARENT='".$r->CAT_ID."'"; 
           $mydb->query($query);
		    echo $r->CAT_NAME."<br>";
		}
	}
}


function insert_category_lv2(){
	global $mydb;
	$rows_lv1 = $mydb->get_results("select * from category where CAT_PARENT = 0");
	$arr_lv1 =array();
	foreach($rows_lv1 as $r1){
		$arr_lv1[] = $r1->CAT_ID;
	}
	$arr_lv1 = implode(",",$arr_lv1);
	$query_lv2 = $mydb->get_results("select * from category where CAT_PARENT > 0 and CAT_PARENT in (".$arr_lv1.")");

	foreach($query_lv2 as $r){

		$rterm = wp_insert_term(
			  $r->CAT_NAME, // the term 
			  'category',
			  array('parent'=> $r->TERM_PARENT)
		);

		
		if(!is_wp_error($rterm)){
			$query = "update `category` set TERM_ID= '".$rterm['term_id']."' where CAT_ID='".$r->CAT_ID."'"; 
			$mydb->query($query);
			$query = "update `category` set TERM_PARENT= '".$rterm['term_id']."' where CAT_PARENT='".$r->CAT_ID."'"; 
           $mydb->query($query);
			echo $r->CAT_NAME."<br>";
           
		}
		
	}
}



function insert_category_lv3(){
	global $mydb;
	$rows_lv1 = $mydb->get_results("select * from category where CAT_PARENT = 0");
	$arr_lv1 =array();
	foreach($rows_lv1 as $r1){
		$arr_lv1[] = $r1->CAT_ID;
	}
	$arr_lv1 = implode(",",$arr_lv1);
	$rows_lv2 = $mydb->get_results("select * from category where CAT_PARENT > 0 and CAT_PARENT in (".$arr_lv1.")");
	
	$arr_lv2 =array();
	foreach($rows_lv2 as $r2){
		$arr_lv2[] = $r2->CAT_ID;
	}
	$arr_lv2 = implode(",",$arr_lv2);
	$rows_lv3 = $mydb->get_results("select * from category where CAT_PARENT > 0 and CAT_PARENT in (".$arr_lv2.")");
	foreach($rows_lv3 as $r){

		$rterm = wp_insert_term(
			  $r->CAT_NAME, // the term 
			  'category',
			  array('parent'=> $r->TERM_PARENT)
		);

		
		if(!is_wp_error($rterm)){
			$query = "update `category` set TERM_ID= '".$rterm['term_id']."' where CAT_ID='".$r->CAT_ID."'"; 
			$mydb->query($query);
			$query = "update `category` set TERM_PARENT= '".$rterm['term_id']."' where CAT_PARENT='".$r->CAT_ID."'"; 
			$mydb->query($query);
           echo $r->CAT_NAME."<br>";
		}
		
	}
}



/* TRANS POST */
function insert_post(){
	global $mydb;
	$page = @$_GET['page'];
	if(!isset($page) || $page <1)
		$page = 0;
		
	$num_limit = 100; 
	$from = $page*$num_limit;
	$rows = $mydb->get_results("select s.*, c.TERM_ID as CATE_TERM_ID ,l.TERM_ID as LIC_TERM_ID from soft1 as s
join category as c on s.CAT_ID = c.CAT_ID
join license as l on s.LICE_ID = l.LICE_ID
ORDER BY soft_id asc
limit ".$num_limit);
//join manufacturer as m on s.MAN_ID = m.MAN_ID
if(count($rows) == 0)
	die("Xong");
	foreach ($rows as $soft) :
		$tags = '';
		$r_tags = $mydb->get_results("select * from soft_tag as st
join tag as t on st.TAG_ID = t.TAG_ID
where st.SOFT_ID = ".$soft->SOFT_ID);

	$results_man =$mydb->get_results("select * from manufacturer WHERE MAN_ID = ".$soft->MAN_ID);
	if($results_man && count($results_man)>0){
		foreach ($results_man as $result_man) :
			$soft->MAN_TERM_ID = $result_man->TERM_ID;
			break;
		endforeach;
	}
	else {
		//mặc định
		$soft->MAN_TERM_ID = 19392;
	}
	
	if($r_tags){
		$arr_tags = array();
		foreach($r_tags as $rt){
		$arr_tags[] = $rt->TAG_NAME;
		}
		$tags = implode(",",$arr_tags);
	}
		$insert = array(
		  'post_title'           => $soft->SOFT_NAME, 
		  'post_content'           => htmlspecialchars_decode($soft->SOFT_CONTENT), 
		  'post_status'           => 'publish', 
		  'post_type'             => 'post',
		  'post_author'           => 1,
		  'ping_status'           => get_option('default_ping_status'), 
		  'post_parent'           => 0,
		  'menu_order'            => 0,
		  'to_ping'               =>  '',
		  'pinged'                => '',
		  'post_password'         => '',
		  'guid'                  => '',
		  'post_content_filtered' => '',
		  'post_excerpt'          => '',
		  'post_category' => array($soft->CATE_TERM_ID),
		  'tags_input' => "$tags",
		  'import_id'             => 0
		);
		
		//wp_set_post_terms($post_id,$man,'manufacturer');
	$post_id =  wp_insert_post($insert);
	update_post_meta($post_id, 'soft_title_description',$soft->SOFT_NAME_DESCRIPTION );
	update_post_meta($post_id, 'soft_filesize',$soft->SOFT_FILESIZE );
	
	update_post_meta($post_id, 'soft_version',$soft->SOFT_VERSION );
	update_post_meta($post_id, 'soft_requirements_os',$soft->SOFT_REQUIREMENT_OS );
	$links = $mydb->get_results("select * from linkdown where SOFT_ID = ".$soft->SOFT_ID);
	
	$thumbnail_id=	_import_photo($post_id,'http://phanmemnhanh.com/'.$soft->SOFT_THUMB);
	set_post_thumbnail( $post_id, $thumbnail_id );
	
	$soft_icon = $soft->SOFT_ICON;
	$icon_id = _import_photo($post_id, 'http://phanmemnhanh.com/'.$soft->SOFT_ICON);
	$soft_icon = wp_get_attachment_image_src($icon_id);
	if($soft_icon){
		$soft_icon = $soft_icon[0];
		//$soft_icon = str_replace("http://phanmemnhanh.com/wp/","http://phanmemnhanh.com/",$soft_icon);
	}
	update_post_meta($post_id, 'soft_icon',$soft_icon );
	
	if($links){
		$arr_link = array();
		$i=0;
		foreach($links as $link){
			$arr_link[$i] = array(
				'link_name'=>$link->LINK_NAME,
				'link_download'=>$link->LINK_LINKDOWN,
			);
			$i++;
		}
		update_post_meta($post_id, 'soft_link_download',json_encode($arr_link));
	}

		wp_set_post_terms($post_id, $soft->MAN_TERM_ID, 'publisher');
		wp_set_post_terms($post_id, $soft->LIC_TERM_ID, 'license');
		$query = "update `soft` set POST_ID= '".$post_id."' where SOFT_ID='".$soft->SOFT_ID."'"; 
			$mydb->query($query);
		
		$query = "delete from `soft1` where SOFT_ID='".$soft->SOFT_ID."'"; 
			$mydb->query($query);
		
		echo $soft->SOFT_NAME."<br>";
			 
 
	endforeach;
	$next_page = $page +1;
	echo' <meta charset="UTF-8">
        <meta http-equiv="refresh" content="3;url=http://phanmemnhanh.com/?page_id=2&page='.$next_page.'">';
}

